<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class UserDao extends Dao implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    function __construct(array $attributes = array()) {
        parent::__construct($attributes);
        $this->table = 'users';
    }

    protected $hidden = array('password', 'remember_token','created_at','updated_at');
    
    public function insertObject($obj){
        $result = DB::insert(DB::raw("insert into users (username, email, name, password, group_id, is_enabled, created_at) ".
                "VALUES ('".$obj->getUsername()."' ".
                        ", '".$obj->getEmail()."' ".
                        ", '".$obj->getName()."' ".
                        ", '".$obj->getPassword()."' ".
                        ", '".$obj->getGroup()->getId()."' ".
                        ", ".$obj->getIsEnabled()." ".
                        ", null ".
                        ")"));
        if (!$result) { return null; }

        return DB::getPdo()->lastInsertId();
    }

    public function updateObject($obj, $old_id){
        DB::update(DB::raw("UPDATE users SET name='".$obj->getName().
                "', username='".$obj->getUsername().
                "', email='".$obj->getEmail().
                "', name='".$obj->getName().
                "', password='".$obj->getPassword().
                "', group_id='".$obj->getGroup()->getId().
                "', is_enabled=".$obj->getIsEnabled().
                ", updated_at=null".
                " WHERE id=".$old_id." "));
    
        return true;
    }
    
    public function deleteObject($id){
        DB::delete(DB::raw("DELETE FROM users where id='".$id."'"));
        
        return true;
    }
    
    public function getObject($id){
        $result = DB::select(DB::raw("SELECT * FROM users where id='".$id."'"));

        if (!$result) { return null; }
        return $this->toObject($result[0]);
    }
    
    public function getList($criteria = NULL){
        $result = DB::select(DB::raw("SELECT * FROM users"));
        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);
        }
            
        return $list;
    }
    
    public function getListByGroup($groupId){
        $result = DB::select(DB::raw("SELECT * FROM users where group_id='$groupId'"));
        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);
        }
            
        return $list;
    }
    
    public function getListByProject($projectId){
        $query = "SELECT users.*, projectassignment.project_id FROM projectassignment inner join users on projectassignment.user_id = users.id";
        if($projectId!=null && $projectId!="")
            $query .= " where projectassignment.project_id='$projectId'";
        $result = DB::select(DB::raw($query));

        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);
        }
            
        return $list;
    }
    
    
    function updateGroupId($groupId,$oldGroupId){
        DB::select( DB::raw("update users set group_id = '$groupId' where group_id = '$oldGroupId' ") );

        return true;
    }
    
    function validatePassword($username,$password){
        $data = parent::where('username', '=' ,$username)->get();
        return Hash::check( $password  ,$data[0]->password );
    }
    
    public function toObject($rowdata=null) {
        $user = new User();
        
        $user->setId($rowdata->id);
        $user->setName($rowdata->name);
        $user->setUsername($rowdata->username);
        $user->setHashedPassword ($rowdata->password);
        $user->setEmail($rowdata->email);
    
        $group = new UserGroup();
        $group->setId($rowdata->group_id);
        $group->setIsLoaded(false);
        $user->setGroup($group);
        
        $user->setIsEnabled($rowdata->is_enabled);
        $user->setLastLogin($rowdata->last_login);
        $user->setIsLoaded(true);

        return $user;
    }
    
}
